package com.da_client_rn.bridge;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.da_client_rn.activity.MainActivity_3;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;

public class BridgeModule extends ReactContextBaseJavaModule {
    private final  String MODULE_NAME = "bridgeModule";
    private  static ReactApplicationContext reactApplicationContext;
    BridgeModule(ReactApplicationContext context) {
        super(context);
        reactApplicationContext = context;
    }
    @NonNull
    @Override
    public String getName() {
        return MODULE_NAME;
    }


    @ReactMethod
    public void openJavaActivity() {
        Intent intent = new Intent(reactApplicationContext, MainActivity_3.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        reactApplicationContext.startActivity(intent);
    }


    @ReactMethod
    public void showToast(String message) {
        Toast.makeText(reactApplicationContext, message, Toast.LENGTH_SHORT).show();
    }


    @ReactMethod
    public void getVersion(Promise promise) {
        try{
            PackageInfo packageInfo = getReactApplicationContext().getPackageManager()
                    .getPackageInfo(getReactApplicationContext().getPackageName(),0);
            String versionName = packageInfo.versionName;
            promise.resolve(versionName);
        }catch (PackageManager.NameNotFoundException e){
            promise.reject("VersionError",e);
        }
    }
}
